package com.xb.loan.usercenter.user.user.service.impl;

import com.xb.loan.log.exception.BizException;
import com.xb.loan.usercenter.service.user.IUserBankCardService;
import com.xb.loan.usercenter.service.user.IUserIdentifyService;
import com.xb.loan.usercenter.service.user.IUserService;
import com.xb.loan.usercenter.service.user.bean.UserIdentifyVO;
import com.xb.loan.usercenter.service.user.bean.UserInfoVO;
import com.xb.loan.usercenter.service.user.constant.UserConst;
import com.xb.loan.usercenter.service.user.exception.IUserServiceException;
import com.xb.loan.usercenter.user.user.dao.UserIdentifyDao;
import com.xb.loan.usercenter.user.user.dao.bean.UserIdentify;
import com.xb.loan.usercenter.user.user.dao.bean.UserInfo;
import com.xb.loan.util.cache.redis.comm.FormatOperation;
import com.xb.loan.util.common.ObjectOperation;
import com.xb.loan.util.log.DefaultLoggerFacotry;
import com.xb.loan.util.mybatis.MyTransactional;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

/**
 * 用户真实信息处理类
 */
@Service("userIdentifyService")
public class UserIdentifyServiceImpl implements IUserIdentifyService,IUserServiceException {
    Logger logger = DefaultLoggerFacotry.getLogger(UserThirdAuthServiceImpl.class);
    @Autowired
	UserIdentifyDao userIdentifyDao;
    
    @Autowired
    private IUserService userService;
    @Autowired
	IUserBankCardService userBankCardService;
    
    @Autowired  //①  注入上下文  
    private ApplicationContext context;      
    private UserIdentifyServiceImpl proxySelf; //②  表示代理对象，不是目标对象
    @PostConstruct  //③ 初始化方法
    private void setSelf() {
        //从上下文获取代理对象（如果通过proxtSelf=this是不对的，this是目标对象）  
        //此种方法不适合于prototype Bean，因为每次getBean返回一个新的Bean  
        proxySelf = (UserIdentifyServiceImpl) context.getBean(IUserIdentifyService.class);   
    }
    
    /**
     *  添加override说明
     **/
    @Override
    public UserIdentifyVO queryByUserId(String userId) throws BizException {
        UserInfoVO userInfoVO = userService.queryUserInfoById(userId);
        UserInfo userInfo = ObjectOperation.convertByClasses(userInfoVO, UserInfo.class);
        if(userInfo==null){
            return null;
        }
        String realId=userInfo.getRealId();
        if(realId==null || realId.length()<=0){
            return null;
        }    
        
        return queryByRealId(realId);
    }

    /**
     *  查询用户实名信息
     *  
     **/
    @Override
    public UserIdentifyVO queryByRealId(String realId) throws BizException {
        UserIdentify userIdentify = userIdentifyDao.selectByPrimaryKey(realId);

        return ObjectOperation.convertByClasses(userIdentify, UserIdentifyVO.class);
    }

    @MyTransactional
	@Override
	public void realName(String userId, UserIdentifyVO userIdentifyVO)
			throws BizException {
    	String realId = "";
		//转换bean属性
		UserIdentify record = ObjectOperation.convertByClasses(userIdentifyVO, UserIdentify.class);
    	
		UserIdentifyVO identifyVO = queryByCardNo(userIdentifyVO.getCardType(),userIdentifyVO.getCardNo());
		if (identifyVO==null) {
			//生成序列号
			realId = FormatOperation.assemblySeq(UserConst.UserSeq.COMMONUSER.getName(), UserConst.UserSeq.COMMONUSER.getCode());
	    	record.setRealId(realId);
			//数据库中插入实名信息
			userIdentifyDao.insert(record);
		}else{
			realId = identifyVO.getRealId();
		}
		//根据userId更新realId信息
		userService.updateRealIdNickName(userId,userIdentifyVO.getRealName(),realId);
		
		//根据userId更新银行卡表里面的card_UserName，不管是否存在都更新为最新的
		userBankCardService.updateCardUserNameByUserId(userId,record.getRealName());
	}
    
	@Override
	public void realName(String userId, String loginName,
                         UserIdentifyVO userIdentifyVO) throws BizException {
		
		userService.updateLoginName(userId, loginName);
		this.realName(userId, userIdentifyVO);
	}

	@Override
	public UserIdentifyVO queryByCardNo(int cardType, String cardNo)
			throws BizException {
		//根据cardType和cardNo查询
		Map<String,String> map = new HashMap<String,String>();
		map.put("cardType", cardType+"");
		map.put("cardNo", cardNo);
		UserIdentify userIdentify = userIdentifyDao.selectRealIdByNameAndCardNo(map);
		//封装数据
		UserIdentifyVO userIdentifyVO = ObjectOperation.convertByClasses(userIdentify, UserIdentifyVO.class);
		return userIdentifyVO;
	}

	@MyTransactional
	@Override
	public void updateUserIdentify(String userId, UserIdentifyVO userIdentifyVO)
			throws BizException {
		realName(userId,userIdentifyVO);
		
	}

	@Override
	public String queryRealnameByUserId(String userId) throws BizException {
		//根据userId查询实名信息
		String realname = userIdentifyDao.selectRealnameByUserId(userId);
		return realname;
	}

	@Override
	public UserIdentifyVO queryByNameAndCardNo(Map<String, String> map)
			throws BizException {
		
		UserIdentify userIdentify = userIdentifyDao.selectRealIdByNameAndCardNo(map);
		if(userIdentify != null){
			
			return ObjectOperation.convertByClasses(userIdentify, UserIdentifyVO.class);
		}
		return null;
	}

	@Override
	public int addUserIdentify(UserIdentifyVO userIdentifyVO)
			throws BizException {
		
		if(userIdentifyVO != null){
			
			UserIdentify userIdentify = ObjectOperation.convertByClasses(userIdentifyVO, UserIdentify.class);
			return userIdentifyDao.insertSelective(userIdentify);
		}
		return 0;
	}

}
